package com.nbsaas.boot.resources;

import com.nbsaas.boot.app.ext.apis.AppMenuExtApi;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.security.api.AuthorizationApi;
import com.nbsaas.boot.shop.api.domain.response.ShopStaffResponse;
import com.nbsaas.boot.shop.ext.domain.response.ShopStaffExtResponse;
import com.nbsaas.boot.utils.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AuthorizationResource implements AuthorizationApi {

    @Resource
    private AppMenuExtApi appMenuExtApi;

    @Transactional
    @Override
    public List<String> loadRoles() {
        return new ArrayList<>();
    }

    @Transactional
    @Override
    public List<String> loadPermissions() {
        List<String> result = new ArrayList<>();
        ShopStaffExtResponse user = UserUtils.user();
        ListResponse<String> permissions = appMenuExtApi.selectPermissionByUser(user.getAppId(), user.getStaff(), user.getShop());
        if (permissions.getData()!=null){
            result.addAll(permissions.getData());
        }
        return result;
    }
}
